<template>
  <div class="video">
    <!-- 内容组件 -->
    <content-template :formData="formData"
                      :searchData="searchData"
                      :tableData="tableData"
                      :getDatas ="getDatas"
                      @handleSubmit="handleSubmit">
    </content-template>
    <!-- 弹框组件 -->
    <dialog-template
            :options = "formData.options.hasAdd">
            <!-- 表单组件 -->
        <form-template
            :formData="formData"
            :options="formData.options.hasAdd"
            :formValidate="formData.formValidate"
            :rules="formData.ruleValidate"
            :list="formData.list"
            @handleSubmit="handleSubmit"
        >             
        </form-template>
    </dialog-template>
  </div>
</template>
<script>
export default {
   data() {
    return {
      formData:{
        index:'',
        options:{
          hasAdd:{  //需不需要新增按钮——为空时不显示
            modal: false,        //弹框状态
            isedit: false,       //是否是编辑状态
            title:'视频' ,      //弹框标题
            width:800,           //弹框宽度
            class:"dialogClass", //弹框类名
            zIndex:100,          //弹框层级
            loading:true,        //弹框加载圈

            formValidate: {    //获取的表单数据
              video_name: '', 
              video_group_id: '',
              image_url:'',
            },
            ruleValidate: {    //验证表单信息
              video_name: [
                {required: true, message: '请填写标题', trigger: 'blur' }
              ],
              video_group_id: [
                {required: true, message: '请选择分类', trigger: 'blur' }
              ],
              image_url: [
                {required: true, message: '请上传图片', trigger: 'blur' }
              ],
            },
          },
          hasSelect:{
            selectList:[
               
            ]
          }       
        },
        list: [ //表单列表——进行一些表单操作
          // {
          //    type: 表单类型,
          //    label: 表单标题，
          //    prop:表单下标指引，要对应formValidate的字段
          //    formStyle:自定义表单的样式
          //    value:传入自定义数据
          // }
          {
            type: "input",       
            prop: "video_name",
            label: "视频标题",
            placeholder:"",
            value:''
          },
          {
            type: "select",       
            prop: "video_group_id",
            label: "视频分类",
            selectData:[],
            placeholder:"",
            value:''
          },
          {
            type: "uploadVideo",      
            prop: "video_url",
            label: "视频链接",
            formStyle:"width:85px;height:125px;line-height:125px"
          },
          {
            type: "uploadImg",      
            prop: "video_image_url",
            label: "视频封面",
            formStyle:"width:85px;height:125px;line-height:125px"
          },
          {
            type: "notes",      
            value: "建议上传尺寸：340 ：508",        
          },
        ],
      },
      searchData:{  
        searchValue: {  //搜索值
          goods_name: '',
        },
        searchColumns: [ //搜索条件和搜索数量——不传则没有
          {
            key: 'name',
            label: "视频标题",
          },
        ]
      },
      tableData:{  //表格数据及操作
        columns: [
            {
                title: '排序',
                key: 'sort',
                width:100,
                align: 'center',
                sortable: true
            },
            {
              title: '视频信息',
              align: 'center',
              width:300,
              render: (h, params) => {
                return h('div',{
                    style: {
                      padding:'10px',
                      display:'flex',
                      alignItems: 'center',
                    }
                  },[
                    h('video', {
                      style: {
                        width: '85px',
                        height: '125px'
                      },
                      attrs: {
                        controls:'controls',
                        poster:this.$webUrl + params.row.video_image_url,
                        src: this.$webUrl + params.row.video_url
                      }
                    }),
                    h('div',{
                        style: {
                          paddingLeft:'10px',
                          textAlign: 'left',
                          lineHeight:'25px'
                        }
                      },[
                        h('h5', {
                          style: {
                            fontSize: '14px',
                            textOverflow: 'ellipsis',
                            color:'#000',
                            display: '-webkit-box',
                            WebkitLineClamp: '2',
                            WebkitBoxOrient: 'vertical'
                          },
                        },params.row.video_name),
                        h('p', {
                          style: {
                            fontSize: '13px',
                            fontWeight: 'bold',
                            color:'#666',
                          },
                        },'点赞：' + params.row.like_count),
                        h('p', {
                          style: {
                            fontSize: '13px',
                            fontWeight: 'bold',
                            color:'#666',
                          },
                        },'评论：' + params.row.discuss_count),
                        h('p', {
                          style: {
                            fontSize: '13px',
                            fontWeight: 'bold',
                            color:'#666',
                          },
                        },'视频分类：' + params.row.video_group.group_name),
                    ])
                ]);
              }
            },
            {
              title: '视频评论',
              key: 'discuss',
              width:200,
              align: 'center',
              render: (h, params) => {
                return h('div', [
                  h('Button', {
                    props: {
                      type: 'primary',
                      size: 'small'
                    },
                    on: {
                      click: () => {
                        this.edit(params.index)
                      }
                    }
                  }, '查看视频评论'),
                ]);
              }
            },
            {
              title: '操作',
              key: 'action',
              align: 'center',
              render: (h, params) => {
                return h('div', [
                  h('Button', {
                    props: {
                      type: 'primary',
                      size: 'small'
                    },
                    on: {
                      click: () => {
                        this.edit(params.index)
                      }
                    }
                  }, '查看关联商品'),
                  h('Button', {
                    props: {
                      type: 'primary',
                      size: 'small'
                    },
                    on: {
                      click: () => {
                        this.edit(params.index)
                      }
                    }
                  }, '修改'),
                  h('Button', {
                    props: {
                      type: 'error',
                      size: 'small'
                    },
                    on: {
                      click: () => {
                        this.deletes(params.index)
                      }
                    }
                  }, '删除'),
                ]);
            }
          }
        ],
        data: [], //表格数据
        total:0,  //表格数据总量
        page:1,   //表格数据页码
        per_page:10,  //表格一页显示几条数据
        loading: true, //表格数据预加载
      }
    }
  },
  mounted() {
    this.getDatas();
    this.getVideogroup()
  },
  methods: {
    getVideogroup(){
      let params = {}
      this.$api.getVideoCategory(params)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.formData.list[1].selectData.map(item=>{
                  item.id = item.video_group_id
                  item.category_name = item.group_name
                  return item
              })
        }
      })
    },
    getDatas(){
      let params = {
        page:this.tableData.page,
        per_page:this.tableData.per_page,
      }
      this.$api.getVideo(params)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.tableData.data = res.data.data
              this.tableData.total = res.data.total
              this.tableData.loading = false
          }
      })
    },
    changeStatus(index){
      let status = "";
      let text = "";
      switch( this.tableData.data[index].status){
        case 1:
          text = "禁用"
          status = 2
          break;
        case 2:
          text = "启用"
          status = 1
          break;
        default:
          console.log('其他')
      };
      let params = {
        video_id: this.tableData.data[index].video_id,
        status: status
      }
      this.$api.statusVideo(params)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$Message.success( text + '成功');
              this.getDatas();
            }
      })
    },
    edit(index){
      
    },
    deletes(index){
      this.$Modal.confirm({
        title: '提示',
        content: '确认删除该视频吗?',
        onOk: () => {
          let params = {
            video_id : this.tableData.data[index].video_id
          }
          this.$api.deletesVideo(params).then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$Message.success('删除成功');
              this.getDatas();
            }
          })
        }
      })
    },
  },
  components: {

  }
}
</script>
<style lang="less" scoped>
    @import './index.less';
</style>
